package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.mapper.ChannelMapper;
import com.heima.media.service.IChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, WmChannel>  implements IChannelService {



    @Override
    public PageResponseResult listByName(ChannelDto channelDto) {
        // 根据频道名称分页查询列表
        // 构建分页条件
        IPage<WmChannel> page=new Page<>(channelDto.getPage(),channelDto.getSize());
        LambdaQueryWrapper<WmChannel> query=new LambdaQueryWrapper<>();
        // 判断频道名称是否为空,不为空,添加查询条件
        if(!StringUtils.isEmpty(channelDto.getName())){
            query.like(WmChannel::getName,channelDto.getName());
        }
        IPage<WmChannel> iPage = this.page(page, query);
        long total = iPage.getTotal();
        List<WmChannel> records = iPage.getRecords();
        // 构建通用的分页响应
        return new PageResponseResult(channelDto.getPage(),channelDto.getSize(),total,records);
    }

    @Override
    public ResponseResult saveWmChannel(WmChannel wmChannel) {
         // 校验参数是否为空
        if(StringUtils.isEmpty(wmChannel.getName())||wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 保存新增的频道
        // 判断频道名称是否已经存在数据库中
        // 根据名称查询
        LambdaQueryWrapper<WmChannel> query=new LambdaQueryWrapper<>();
        query.eq(WmChannel::getName,wmChannel.getName());
        int count = this.count(query);
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        wmChannel.setCreatedTime(new Date());
        this.save(wmChannel);

        return ResponseResult.okResult();
    }
    @Autowired
    ChannelMapper channelMapper;


    @Override
    public ResponseResult deleteById(Integer id) {
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel wmChannel = channelMapper.selectById(id);
        System.out.println(wmChannel.getStatus());
        if(!wmChannel.getStatus()){
            channelMapper.deleteById(id);
            return ResponseResult.okResult();
        }
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

    }
}
